package com.spzx.product.controller;


import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌Controller
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    IBrandService brandService;

    /**
     * 查询品牌列表
     * @param brand 品牌信息
     * @return 品牌信息列表
     */
    @Operation(summary = "查询品牌列表")
    @GetMapping("list")
    public TableDataInfo list(Brand brand){
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    /**
     * 获取品牌详细信息
     * @param id 品牌主键
     * @return 品牌信息
     */
    @Operation(summary = "获取品牌详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id){
        return success(brandService.selectBrandById(id));
    }

    /**
     * 新增品牌
     * @param brand 品牌信息
     * @return 返回信息
     */
    @Operation(summary = "新增品牌")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated Brand brand){
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.insertBrand(brand));
    }

    /**
     * 修改品牌
     * @param brand 品牌信息
     * @return 返回信息
     */
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated Brand brand){
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateBrand(brand));
    }

    /**
     * 删除品牌
     * @param ids 品牌ids
     * @return T F
     */
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(brandService.deleteBrandByIds(ids));
    }

    /**
     * 获取全部品牌
     * @return 品牌信息列表
     */
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAll")
    public AjaxResult getBrandAll(){
        return success(brandService.selectBrandAll());
    }

    //=============给前台使用的服务接口=======================================================

    /**
     * 获取全部品牌
     * @return 品牌信息列表
     */
    @InnerAuth
    @Operation(summary = "获取全部品牌")
    @GetMapping("getBrandAllList")
    public R<List<Brand>> getBrandAllList(){
        return R.ok(brandService.selectBrandAll());
    }
}


